package com.stud.framework.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import com.stud.common.jdbc.MyJdbcTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

/**
 * druid 配置多数据源
 *
 * @author Stud
 */
@Configuration
public class DruidConfig {
    @Primary
    @Bean(name = "studDataSource")
    @ConfigurationProperties("spring.datasource.druid.stud")
    public DruidDataSource studDataSource() {
        return DruidDataSourceBuilder.create().build();
    }

    @Primary
    @Bean(name = "studJdbcTemplate")
    public MyJdbcTemplate studJdbcTemplate() {
        MyJdbcTemplate jdbcTemplate = new MyJdbcTemplate();
        jdbcTemplate.setDataSource(studDataSource());
        return jdbcTemplate;
    }

    @Primary
    @Bean(name = "studTransactionManager")
    public DataSourceTransactionManager studTransactionManager(@Qualifier("studDataSource") DruidDataSource druidDataSource) {
        DataSourceTransactionManager tm = new DataSourceTransactionManager(druidDataSource);
        tm.setNestedTransactionAllowed(true);
        return tm;
    }

    @Bean(name = "xxljobDataSource")
    @ConfigurationProperties("spring.datasource.druid.xxljob")
    public DruidDataSource xxljobDataSource() {
        return DruidDataSourceBuilder.create().build();
    }

    @Bean(name = "xxljobJdbcTemplate")
    public MyJdbcTemplate xxljobJdbcTemplate() {
        MyJdbcTemplate jdbcTemplate = new MyJdbcTemplate();
        jdbcTemplate.setDataSource(xxljobDataSource());
        return jdbcTemplate;
    }

//    @Bean(name = "huafeiDataSource")
//    @ConfigurationProperties("spring.datasource.druid.huafei")
//    public DruidDataSource huafeiDataSource() {
//        return DruidDataSourceBuilder.create().build();
//    }
//
//    @Bean(name = "huafeiJdbcTemplate")
//    public MyJdbcTemplate huafeiJdbcTemplate() {
//        MyJdbcTemplate jdbcTemplate = new MyJdbcTemplate();
//        jdbcTemplate.setDataSource(huafeiDataSource());
//        return jdbcTemplate;
//    }

}
